package com.sky.directdeal.activity;

import java.util.List;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.ListView;

import com.sky.directdeal.R;
import com.sky.directdeal.adapter.DealListAdapter;
import com.sky.directdeal.constants.Constants;
import com.sky.directdeal.listener.ServiceResponseListener;
import com.sky.directdeal.manager.ServiceManager;
import com.sky.local_ads.offer_list.OfferResponse;
import com.sky.local_ads.offer_list.OffersList;

	public class OfferListActivity extends BaseActivity {
		private ListView offersListView;
		private DealListAdapter dealListAdapter;
		private boolean isService=false;

		@Override
		protected void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.activity_offer_list);
			setHomeUp();
			setHeaderTitle(getResources().getString(R.string.today_deal));
			offersListView=(ListView)findViewById(R.id.businessDealList);
			getBundleData(this);
			
		}
		
		private void getBundleData(Context context) {
			if (getIntent().getExtras() != null
					&& getIntent().hasExtra(Constants.INTENT_CATEGORYY_ID)) {
					int catId = getIntent().getIntExtra(Constants.INTENT_CATEGORYY_ID, -1);
					if (!isService) {
						isService = true;
						showProgressDialog();
						ServiceManager.getStoreOffersByBusiness(OfferListActivity.this, String.valueOf(catId), offersResponseListener);
					}
				}
		}

	private ServiceResponseListener<OfferResponse> offersResponseListener=new ServiceResponseListener<OfferResponse>() {
			
			@Override
			public void onSuccess(OfferResponse response) {
				mainCategoryHandler.sendEmptyMessage(Constants.DISMISS_PROGRESS_DIALOG);
				isService=false;
				
				if(response!=null && response.getStoreOffersList()!=null){
					
					if( response.getStoreOffersList().size()>0){
						List<OffersList> offerList=response.getStoreOffersList();
						dealListAdapter=new DealListAdapter(OfferListActivity.this, offerList);
						offersListView.setAdapter(dealListAdapter);
						
					}else{
						showAlert(OfferListActivity.this, getString(R.string.alert), getString(R.string.no_offers_found), getString(R.string.ok_button), positiveListener, null, null);
					}
					
				}else{
					showAlert(OfferListActivity.this, getString(R.string.alert), getString(R.string.no_offers_found), getString(R.string.ok_button), positiveListener, null, null);
				}
			}
			
			@Override
			public void onFailure(Throwable throwable, String errorResponse) {
				mainCategoryHandler.sendEmptyMessage(Constants.DISMISS_PROGRESS_DIALOG);
				isService=false;
				showAlert(OfferListActivity.this, getString(R.string.server_failed), getString(R.string.server_unavailable), getString(R.string.ok_button), positiveListener, null, null);
				
			}
		};
		
		 public DialogInterface.OnClickListener positiveListener=new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					finish();
				}
			};
		
	
		@Override
		public boolean onOptionsItemSelected(MenuItem item) {
			switch (item.getItemId()) {
			case android.R.id.home:
				finish();
				break;

			default:
				break;
			}
			return super.onOptionsItemSelected(item);
		}

	}
